package com.lhg.service;

import com.lhg.entity.Info;
import com.lhg.entity.Notice;
import com.lhg.entity.Vo.AcVo;
import com.lhg.entity.Vo.BarChartVo;
import com.lhg.entity.Vo.InfoVo;
import com.lhg.entity.Vo.PieChartVo;
import com.lhg.mapper.HomeMapper;
import com.lhg.mapper.InfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class HomeService {
    @Autowired
    private HomeMapper homeMapper;
    @Autowired
    private InfoMapper infoMapper;

    public List<Notice> selectAllNotice() {
        return homeMapper.selectAllNotice();
    }

    public List<AcVo> selectAllAc(Integer userId) {
        List<AcVo> acVoList = new LinkedList<>();
        AcVo acvo1 = new AcVo();
        acvo1.setTitle("投递总数");
        acvo1.setValue(String.valueOf(infoMapper.selectByUserId(userId).size()));
        AcVo acvo2 = new AcVo();
        acvo2.setTitle("最近笔试日期");
        InfoVo infoVo = new InfoVo();
        // 获取今天的日期时间，并设置为0点
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime todayMidnight = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        String todayMidnightString = todayMidnight.format(formatter);
        infoVo.setStart(todayMidnightString);
        List<Info> infoList = infoMapper.selectExam(infoVo,userId);
        if(infoList.size()<=0){
            acvo2.setValue("未来无笔试");
        }else{
            List<String> examDate = new LinkedList<>();
            for(Info info : infoList){
                examDate.add(info.getExamTime());
            }
            Collections.sort(examDate);
            acvo2.setValue(examDate.get(0));
        }
        AcVo acvo3 = new AcVo();
        acvo3.setTitle("最近面试日期");
        infoList = infoMapper.selectInterview(infoVo,userId);
        if(infoList.size()<=0){
            acvo3.setValue("未来无面试");
        }else{
            List<String> interviewDate = new LinkedList<>();
            for(Info info : infoList){
                interviewDate.add(info.getInterviewTime());
            }
            Collections.sort(interviewDate);
            acvo3.setValue(interviewDate.get(0));
        }
        AcVo acvo4 = new AcVo();
        acvo4.setTitle("已结束offer数");
        acvo4.setValue(String.valueOf(infoMapper.selectOffers(todayMidnightString,userId).size()));
        acVoList.add(acvo1);
        acVoList.add(acvo2);
        acVoList.add(acvo3);
        acVoList.add(acvo4);
        return acVoList;
    }

    public List<PieChartVo> selectSizeCount(Integer userId) {
        List<PieChartVo> list = new ArrayList<>();
        List<Info> infoList = infoMapper.selectByUserId(userId);
        PieChartVo pieChartVo1 = new PieChartVo();
        pieChartVo1.setName("Big");
        int size1 = 0;
        int size2 = 0;
        int size3 = 0;
        PieChartVo pieChartVo2 = new PieChartVo();
        pieChartVo2.setName("Meida");
        PieChartVo pieChartVo3 = new PieChartVo();
        pieChartVo3.setName("Small");
        Pattern pattern = Pattern.compile("\\d+"); // 匹配一个或多个数字
        for(Info info : infoList){
            Matcher matcher = pattern.matcher(info.getSize());
            if (matcher.find()) {
                int number = Integer.parseInt(matcher.group());
                if (number>1000){
                    size1++;
                }else if(number>100){
                    size2++;
                }else{
                    size3++;
                }
            }
        }
        pieChartVo1.setValue(size1);
        pieChartVo2.setValue(size2);
        pieChartVo3.setValue(size3);
        list.add(pieChartVo1);
        list.add(pieChartVo2);
        list.add(pieChartVo3);
        return list;
    }

    public List<BarChartVo> interviewCount(Integer userId) {
        List<BarChartVo> list = new ArrayList<>();
        BarChartVo barChartVo1 = new BarChartVo();
        barChartVo1.setDate("Today");
        BarChartVo barChartVo2 = new BarChartVo();
        barChartVo2.setDate("Tomorrow");
        BarChartVo barChartVo3 = new BarChartVo();
        barChartVo3.setDate("Next");
        BarChartVo barChartVo4 = new BarChartVo();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime todayMidnight = LocalDateTime.now();
        String today = todayMidnight.format(formatter);
        LocalDateTime lastM = todayMidnight.plusDays(3).truncatedTo(ChronoUnit.DAYS);
        String last = lastM.format(formatter);
        barChartVo4.setDate(last);

        LocalDateTime Tomorrow = todayMidnight.plusDays(1).truncatedTo(ChronoUnit.DAYS);
        String tomorrow = Tomorrow.format(formatter);
        LocalDateTime Next = todayMidnight.plusDays(2).truncatedTo(ChronoUnit.DAYS);
        String next = Next.format(formatter);
        List<Info> examList = infoMapper.selectCountExam(today,userId);
        List<Info> interviewList = infoMapper.selectCountInterview(today,userId);
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        for(Info info : examList){
            String cur = info.getExamTime().split(" ")[0];
            if(cur.equals(today)){
                count1++;
            }else if(cur.equals(tomorrow)){
                count2++;
            }else if(cur.equals(next)){
                count3++;
            }else if(cur.equals(last)){
                count4++;
            }
        }
        barChartVo1.setExam(count1);
        barChartVo2.setExam(count2);
        barChartVo3.setExam(count3);
        barChartVo4.setExam(count4);

        count1 = 0;
        count2 = 0;
        count3 = 0;
        count4 = 0;
        for(Info info : interviewList){
            String cur = info.getInterviewTime().split(" ")[0];
            if(cur.equals(today)){
                count1++;
            }else if(cur.equals(tomorrow)){
                count2++;
            }else if(cur.equals(next)){
                count3++;
            }else if(cur.equals(last)){
                count4++;
            }
        }
        barChartVo1.setInterview(count1);
        barChartVo2.setInterview(count2);
        barChartVo3.setInterview(count3);
        barChartVo4.setInterview(count4);
        list.add(barChartVo1);
        list.add(barChartVo2);
        list.add(barChartVo3);
        list.add(barChartVo4);
        return list;
    }
}
